package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.client.BaseCustomerSupplier;
import com.ruoyi.system.mapper.ClientSupplierMapper;
import com.ruoyi.system.service.IClientSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author GE-LV5
 * @create 2023/3/14 15:07
 */
@Service
public class ClientSupplierServiceImpl extends ServiceImpl<ClientSupplierMapper, BaseCustomerSupplier> implements IClientSupplierService {

    @Autowired
    ClientSupplierMapper clientSupplierMapper;

    @Override
    public String importArea(List<BaseCustomerSupplier> areaList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(areaList) || areaList.size() == 0)
        {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        //String password = configService.selectConfigByKey("sys.user.initPassword");
        for (BaseCustomerSupplier product : areaList)
        {
            try
            {
                // 验证是否存在这个生产订单
                BaseCustomerSupplier sa = clientSupplierMapper.selectClientSupplierList(product.getCode());
                if (StringUtils.isNull(sa))
                {
                    product.setCreateBy(operName);
                    this.insertClientSupplier(product);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、客户供应商 " + product.getCode() + " 导入成功");
                }
                else if (updateSupport)
                {
                    product.setUpdateBy(operName);
                    this.updateClientSupplier(product);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、客户供应商 " + product.getCode() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、客户供应商 " + product.getCode() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、客户供应商 " + product.getCode() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }



    @Override
    public int insertClientSupplier(BaseCustomerSupplier clientSupplier) {
        return clientSupplierMapper.insertClientSupplier(clientSupplier);
    }

    @Override
    public int updateClientSupplier(BaseCustomerSupplier clientSupplier) {
        return clientSupplierMapper.updateClientSupplier(clientSupplier);
    }

    @Override
    public List<BaseCustomerSupplier> selectListOrdersFordTempList(BaseCustomerSupplier clientSupplier) {
        return clientSupplierMapper.selectListOrdersFordTempList(clientSupplier);
    }
}
